package prepare_data

import (
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 准备数据：20日方差最少的(10*账号数量+4)只股票
type MinVariance20PrepareData struct {
	db *gorm.DB
}

// 初始化
func (m *MinVariance20PrepareData) Init(db *gorm.DB) {
	m.db = db
}

// 准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照variance20升序排列，包括正在持有的股票
func (m *MinVariance20PrepareData) DoPrepareData(currentDate string) {
	io.Infoln("准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照variance20升序排列，包括正在持有的股票")

	// 清空quant_stock_filter表
	m.db.Exec("truncate table quant_stock_filter")

	// 从stock_transaction_data_all表中向quant_stock_filter表中插入记录，按照variance20升序排列，包括正在持有的股票
	m.db.Exec("insert /*+ append */ into quant_stock_filter(stock_code, variance20, total_market_value) "+
		"select distinct stda.code_, stda.variance20, stda.total_market_value "+
		"from stock_transaction_data_all stda "+
		"where stda.date_=to_date(?, 'yyyy-mm-dd') and stda.variance20 is not null "+
		"order by stda.variance20 asc", currentDate)
}
